package com.ruoyi.base.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.base.domain.DefaultAccountSet;
import com.ruoyi.base.domain.Industry;
import com.ruoyi.base.mapper.DefaultAccountSetMapper;
import com.ruoyi.base.mapper.IndustryMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.AccountSetMapper;
import com.ruoyi.base.domain.AccountSet;
import com.ruoyi.base.service.IAccountSetService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账套列表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-23
 */
@Service
public class AccountSetServiceImpl implements IAccountSetService {
    @Autowired
    private AccountSetMapper accountSetMapper;

    @Autowired
    private IndustryMapper industryMapper;

    @Autowired
    private DefaultAccountSetMapper defaultAccountSetMapper;

    /**
     * 查询账套列表
     *
     * @param accountId 账套列表主键
     * @return 账套列表
     */
    @Override
    public AccountSet selectAccountSetByAccountId(Long accountId) {
        return accountSetMapper.selectAccountSetByAccountId(accountId);
    }

    /**
     * 查询账套列表列表
     *
     * @param accountSet 账套列表
     * @return 账套列表
     */
    @Override
    public List<AccountSet> selectAccountSetList(AccountSet accountSet) {
        return accountSetMapper.selectAccountSetList(accountSet);
    }

    /**
     * 新增账套列表
     *
     * @param accountSet 账套列表
     * @return 结果
     */
    @Override
    public int insertAccountSet(AccountSet accountSet) {
        //判断名称是否重复
        String accountName = accountSet.getAccountName();
        List<AccountSet> accountSets1 = accountSetMapper.selectList(new QueryWrapper<AccountSet>().lambda().eq(AccountSet::getAccountName, accountName));

        if (accountSets1.size() > 0) {
            throw new RuntimeException("名称已存在，不可使用");
        }

        return accountSetMapper.insert(accountSet);
    }

    /**
     * 修改账套列表
     *
     * @param accountSet 账套列表
     * @return 结果
     */
    @Override
    public int updateAccountSet(AccountSet accountSet) {

        return accountSetMapper.updateAccountSet(accountSet);
    }

    /**
     * 批量删除账套列表
     *
     * @param accountIds 需要删除的账套列表主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetByAccountIds(Long[] accountIds) {
        return accountSetMapper.deleteAccountSetByAccountIds(accountIds);
    }

    /**
     * 删除账套列表信息
     *
     * @param accountId 账套列表主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetByAccountId(Long accountId) {
        return accountSetMapper.deleteAccountSetByAccountId(accountId);
    }

    @Override
    public List<Industry> getIndustryList() {
        List<Industry> list = industryMapper.selectIndustryList(null);
        return list;
    }

    @Override
    public List<AccountSet> getAccountSetListAll() {

        List<AccountSet> accountSets = accountSetMapper.selectList(null);

        return accountSets;
    }

    @Override
    public AccountSet getDefaultAccountSet() {

        LoginUser loginUser = SecurityUtils.getLoginUser();


        return accountSetMapper.getDefaultAccountSet(loginUser.getUserid());
    }

    @Override
    @Transactional
    public int chaneDefaultStatus(Long accountId) {
        //1.使用当前登录用户id 和 当前默认的账套id 进行查询
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();
        QueryWrapper<DefaultAccountSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DefaultAccountSet::getUserId, userid);
        DefaultAccountSet defaultAccountSet = defaultAccountSetMapper.selectOne(queryWrapper);
        if (defaultAccountSet == null) {
            //没有的话设置默认值
            int insert = defaultAccountSetMapper.insert(DefaultAccountSet.builder().accountSetId(accountId).userId(userid).build());
            return insert;
        }

        if (defaultAccountSet.getAccountSetId().equals(accountId)) {
            throw new RuntimeException("当前账套正在使用，不可重复设置");
        }

        // 删除原有的，新增新的默认
        int i = defaultAccountSetMapper.deleteById(defaultAccountSet.getId());
        int insert = defaultAccountSetMapper.insert(DefaultAccountSet.builder().accountSetId(accountId).userId(userid).build());

        if (i > 0 && insert > 0) {
            return 1;
        }
        return 0;
    }
}
